package stephen.feb3;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class TestJsp
 */
public class TestJsp extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestJsp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("do Get"+request.getParameter("getvalue"));
		//查询用get
		//9大jsp内置对象 1、request范围：用户当前的请求
		System.out.println("request.getContextPath()=="+request.getContextPath());
		System.out.println("request.getContextPath()=="+request.getRemoteAddr());
		System.out.println("request.getRequestedSessionId())=="+request.getRequestedSessionId());
		System.out.println("request.getRequestedSessionId())"+request.getProtocol());
		System.out.println("request.getRemoteHost()=="+request.getRemoteHost());
		//2、response 对客户端的响应。向客户端发送数据。
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setCharacterEncoding("utf-8");
		response.getWriter().append("登录成功");
		
		//重定向的方式做页面跳转。相当于直接让浏览器访问另一个地址，浏览器地址变换
		response.sendRedirect("");
		
		//请求转发的方式做页面跳转.相当于浏览器的地址不变，指示浏览器加载的内容，变成了新的浏览器内容
		request.getRequestDispatcher("").forward(request, response);
		
		//3、session 只要客户不关闭浏览器，session里面存的值就一直存在
		HttpSession as=request.getSession();
		String name="cx";
		as.setAttribute("name", name);
		
		//4、application 服务器启动，就会创建一个application对象，只要服务器没有关闭，里面存的值就一直在。
		ServletContext application=this.getServletContext();
		//application.setAttribute("test", 100);;
		
		//5、Out 向客户端发送数据。
		PrintWriter out=response.getWriter();
		out.println("com from out.print");
		
		//6.cookie 
		Cookie ck=new Cookie("aa","100");
		ck.setMaxAge(1000);
		response.addCookie(ck);
		
		//7、config
				
		//8、Page（PageContext）
		
		//9、Exception
		
		//四大作用域
		//1、page，里面存的值只能在当前页面，只要发生页面跳转，里面值就没了
        //2、request 里面存的值可以跨越forward的前后两个页面，只要刷新页面，里面的值就没了。
		//3、session 只要不关闭浏览器，里面存的值就在，一旦关闭浏览器，里面存的值就没了
		//4、application 只要不关闭服务器，里面存的值就一直在，一旦关闭服务器，里面存的值就没了
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		System.out.println("do Post"+request.getParameter("Postvalue"));
	}

}
